#include "Event.h"
#include "../common/Constantes.h"

Event::Event() {}

Event::Event(SDL_Event event){
	if(event.type == SDL_USEREVENT){
		this->event = event;
	}


	this->event = event;
}

Event::Event(DTOShipStatus* dtoShip){
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = Ship_Status;
	event.user.data1 = new std::string(dtoShip->getOwner());
	event.user.data2 = new std::string(dtoShip->serializar());

	this->event = event;
	this->sender = dtoShip->getOwner();
}

Event::Event(DTOShipAction* dtoShipAction){
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = Ship_Action;
	event.user.data1 = new std::string(dtoShipAction->serializar());
	event.user.data2 = NULL;
	this->event = event;
	this->sender = dtoShipAction->getOwner();
}

Event::Event(DTOAsteroide* dtoAsteroide){
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = Asteroid_Status;
	event.user.data1 = new std::string(dtoAsteroide->serializar());
	event.user.data2 = NULL;
	this->event = event;
	this->sender = dtoAsteroide->getOwner();
}

Event::Event(DTOExplosion* dtoExplosion){
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = New_Explosion;
	event.user.data1 = new std::string(dtoExplosion->serializar());
	event.user.data2 = NULL;
	this->event = event;
	this->sender = dtoExplosion->getOwner();
}

Event::Event(DTOBonus* dtoBonus){
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = New_Bonus;
	event.user.data1 = new std::string(dtoBonus->serializar());
	event.user.data2 = NULL;
	this->event = event;
	this->sender = dtoBonus->getOwner();
}

Event::Event(DTOEvent* dtoEvent) {
	std::stringstream ss;
	ss << "tipo de evento: " << dtoEvent->type;
	Logger::trace("Event", "constructor", &ss);

	// Armo un SDL_Event
	SDL_Event event;
	event.user.data1 = NULL;
	event.user.data2 = NULL;

	switch (dtoEvent->type) {
	case KeyPressed:
		event.type = SDL_USEREVENT;
		event.user.code = UserKeyPressed;
		event.key.keysym.unicode = dtoEvent->keyUnicode;
		event.key.keysym.sym = (SDLKey)dtoEvent->key;
		event.button.state = dtoEvent->buttonState;

		this->sender = dtoEvent->getOwner();

		break;

	case KeyReleased:
		event.type = SDL_USEREVENT;
		event.user.code = UserKeyReleased;
		event.key.keysym.unicode = dtoEvent->keyUnicode;
		event.key.keysym.sym = (SDLKey)dtoEvent->key;
		event.button.state = dtoEvent->buttonState;

		this->sender = dtoEvent->getOwner();

		break;

	default:
		break;
	}

	this->event = event;
}

Event::~Event() {
	// TODO Auto-generated destructor stub
}


void Event::setSender(std::string sender) {
	this->sender = sender;
}

EventType Event::getType() {
	return (EventType) this->event.type;
}

std::string Event::getTypeDescription(){

	switch(event.user.code)
	{
	case NewUser:
		return "NewUser";
		break;
	case Ship_Status:
		return "Ship_Status";
		break;
	case UserSyncronize:
		return "UserSyncronize";
		break;
	case UserDisconected:
		return "UserDisconected";
		break;
	case ItemList_UpButton:
		return "ItemList_UpButton";
		break;
	case ItemList_DownButton:
		return "ItemList_DownButton";
		break;
	default:
		return UtilString::intToString(event.user.code);
	}

}

EventKey Event::getKey() {
	return (EventKey) this->event.key.keysym.sym;
}

EventButtonState Event::getButtonState() {
	return (EventButtonState) this->event.button.state;
}

EventMouseButton Event::getMouseButton() {
	return (EventMouseButton) this->event.button.button;
}

WindowID Event::getCode() {
	return (WindowID) this->event.user.code;
}

std::string Event::getData() {
	return *(std::string*) this->event.user.data1;
}

/*
void Event::deleteData(){
	delete(std::string*)this->event.user.data1;
}

void Event::deleteData2(){
	delete(std::string*)this->event.user.data2;
}
*/

std::string Event::getData2() {
	return *(std::string*) this->event.user.data2;
}


Uint16 Event::getKeyUnicode() {
	return (Uint16) this->event.key.keysym.unicode;
}

Uint16 Event::getXCoordinate() {
	return (Uint16) this->event.button.x;
}

Uint16 Event::getYCoordinate() {
	return (Uint16) this->event.button.y;
}
SDL_Event* Event::getEvent() {
	return &(this->event);

}

Uint16 Event::getW() {
	return (Uint16) (this->event.resize.w);
}

Uint16 Event::getH() {
	return (Uint16) (this->event.resize.h);
}


DTOEvent* Event::transformInDTO() {
	return new DTOEvent(getKeyUnicode(), getType(), getH(), getW(),
			getXCoordinate(), getYCoordinate(), getCode(), getButtonState(),
			getKey(), sender);
}

DTOShipStatus* Event::transformInDTOShipStatus(){

	std::string data1 = getData();
	std::string data2 = getData2();

	delete(std::string*)this->event.user.data1;
	delete(std::string*)this->event.user.data2;

	return new DTOShipStatus(data2,data1);
}

std::string Event::getSender() {
	return sender;
}
